#include <windows.h>
#include <gl/glut.h>	/* this includes the others */
#include <stdio.h>
#include <cmath>

#include <iostream>
#include "objloader.h"
#include "Game.h"
#include "glm/glm.h"
#include "ViewFactory.h"
#include "ObjectFactory.h"


Game* game;


int theta = 0;
void myReshape (int w, int h) {
	float ratio = 1.0 * w / h;

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glViewport(0,0,w,h);
	//glOrtho(-2000, 2000, -500, 500, 100, 1000);
	gluPerspective(120, ratio, 0, 1000000);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	gluOrtho2D(0, 10, 0, 10);
}


void display (void) {
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f (1.0f, 1.0f, 1.0f);

	game->Draw();

	glutSwapBuffers ();
}

void keyboard(unsigned char key, int _x, int _y)
{
   switch (key) {
	case 's':
		if(!game->is_game())
		{
			game->GameStart();
		}
		break;
	case '1':
		if(game->is_game())
		{
			game->changeView(VIEW_ONE);
		}
		break;
	case '2':
		if(game->is_game())
		{
			game->changeView(VIEW_TWO);
		}
		break;
	case '3':
		if(game->is_game())
		{
			game->changeView(VIEW_THREE);
		}
		break;
	case '4':
		if(game->is_game())
		{
			game->changeView(VIEW_FOUR);
		}
		break;
	case ' ':
		if(game->is_game())
		{
			game->Shoot();
		}
		break;
	case 'a':
		if(game->is_game())
		{
			game->rotate(3, 0);
		}
		break;
	case 'b':
		if(game->is_game())
		{
			game->rotate(-3, 0);
		}
		break;
	case 'c':
		if(game->is_game())
		{
			game->rotate(0, 3);
		}
		break;
	case 'd':
		if(game->is_game())
		{
			game->rotate(0, -3);
		}
		break;
   }
}

void MyTimer (int value) {
	game->Moving();
	glutPostRedisplay();
	glutTimerFunc(10,MyTimer,1);
}

void GenerateTimer(int value)
{
	if(game->is_game())
		ObjectFactory::generateObjects(20000);
	glutTimerFunc(10000, GenerateTimer, 0);
}

void main (int argc, char **argv) 
{	
	game = new Game();
	
    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(800, 600);
    glutCreateWindow ("simple");
    glutReshapeFunc (myReshape);
    glutDisplayFunc (display);
	glutKeyboardFunc(keyboard);
	glutTimerFunc(10, MyTimer, 1);
	glutTimerFunc(5000, GenerateTimer, 0);
	glEnable(GL_DEPTH_TEST);
    glutMainLoop();
}